<?php

namespace App\Admin\Controllers;

use App\Models\Order;
use App\Http\Controllers\Controller;
use App\Models\OrderPhone;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Layout\Content;
use App\Admin\Extensions\CheckAchieve;
use App\Admin\Extensions\CheckCancel;
use App\Admin\Extensions\CheckRemit;
use App\Admin\Extensions\CheckToRemit;
use App\Admin\Extensions\CheckToCancel;
use Illuminate\Http\Request;

class AcknowledgedOrdersController extends Controller
{
    use HasResourceActions;

    //列表
    public function index(Content $content)
    {
        return Admin::content(function (Content $content) {
            $content->header('已确认订单列表');
            $content->body($this->grid());
        });
    }

    //查看详情
    public function show($id, Content $content)
    {
        return $content
            ->header('已确认订单')
            ->description('详情')
            ->body($this->detail($id));
    }

    //订单列表
    protected function grid()
    {
        return Admin::grid(Order::class, function (Grid $grid) {
            $grid->id('ID')->sortable();
            $grid->user()->name('下单用户');
            $grid->order_no('订单编号');
            $grid->information('商品信息');
            $grid->amount('下单数量');
            $grid->create_at('下单时间');
            $grid->logistics_company('物流公司');
            $grid->tracking_number('物流单号');
            $grid->is_delivery('是否发货')->display(function ($value) {
                return ($value==1) ? '已发货' : '未发货';
            });
            $grid->is_receiving('是否收货')->display(function ($value) {
                return ($value==1) ? '已收货' : '未收货';
            });

            $grid->model()->orderBy('create_at', 'desc');

            $grid->model()->where('is_receiving', '1');
            $grid->model()->where('is_trial', '1');
            $grid->model()->where('is_recheck', '1');
            $grid->model()->where('is_recheck_confirmed', '1');
            $grid->model()->where('is_whole_confirmed', '1');
            $grid->model()->where('is_achieve_confirmed', '0');

            $grid->actions(function ($actions) {
                $actions->disableDelete();
                $actions->disableEdit();

                // append一个操作
                if($actions->row->is_remit == 0 && $actions->row->is_cancel == 0) {
                    $actions->append(new CheckRemit($actions->getKey()));
                    $actions->append(new CheckCancel($actions->getKey()));
                }else{
                    $actions->append(new CheckAchieve($actions->getKey()));
                }
            });

            $grid->tools(function ($tools) {
                // 禁用批量删除按钮
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });

            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();

                // 在这里添加字段过滤器
                $filter->column(1/3, function ($filter) {
                    $filter->like('order_no', '订单编号');
                });
                $filter->column(1/3, function ($filter) {
                    $filter->like('tracking_number', '物流单号');
                });
            });

            $grid->disableCreateButton();

            $grid->disableRowSelector();

            //禁用导出按钮
            $grid->disableExport();
        });
    }


    /**
     * 订单详情
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Order::findOrFail($id));

        $show->id('ID')->sortable();
        $show->order_no('订单编号');
        $show->information('商品信息');
        $show->amount('下单数量');
        $show->create_at('下单时间');
        $show->logistics_company('物流公司');
        $show->tracking_number('物流单号');
        $show->is_delivery('是否发货')->as(function ($value) {
            return ($value==1) ? '已发货' : '未发货';
        });
        $show->picture('商品图片')->unescape()->as(function ($value) {
            $img = '';
            if(!empty($value)) {
                $value = explode(',', $value);
                foreach ($value as $k => $v) {
                    $img .= "<img src='{$v}' style='width: 200px;height: 200px;'/>";
                }
            }
            return $img;
        });

        $show->user('用户信息', function ($user) {
            $user->name('用户名称');
            $user->phone('用户电话');

            $user->panel()->tools(function ($tools) {
                $tools->disableDelete();
                $tools->disableEdit();
                $tools->disableList();
            });
        });

        $show->phone('子订单', function ($phone) {
            $phone->resource('/admin/order_phone');

            $phone->id('ID');
//            $phone->title('简介');
            $phone->phone_model('手机型号');
            $phone->note('备注');
            $phone->imei('IMEI码');
            $phone->level('等级');
            $phone->code('机器标码');
            $phone->price('价格');
            $phone->logistics_company('已退回订单选择物流公司');
            $phone->tracking_number('已退回订单选择物流单号');

            $phone->disableExport();
            $phone->disableRowSelector();
            $phone->disableCreateButton();

            $phone->is_confirm('状态')->display(function ($value) {
                switch ($value) {
                    case '0':
                        return '未确认';
                        break;
                    case '1':
                        return '待确认';
                        break;
                    case '2':
                        return '已确认';
                        break;
                    case '3':
                        return '已取消';
                        break;
                }
            });

            $phone->is_remit('是否已打款')->display(function ($value) {
                if($this->is_confirm == '2') {
                    switch ($value) {
                        case '0':
                            return '打款中';
                            break;
                        case '1':
                            return '已打款';
                            break;
                    }
                }
            });

            $phone->is_send_back('是否已寄回')->display(function ($value) {
                if($this->is_confirm == '3') {
                    switch ($value) {
                        case '0':
                            return '待寄回';
                            break;
                        case '1':
                            return '已寄回';
                            break;
                    }
                }
            });

            $phone->actions(function ($actions) {
                $actions->disableView();
                $actions->disableDelete();

                if($actions->row->is_confirm == 2 || $actions->row->is_confirm == 3)
                if($actions->row->is_confirm == 2 && $actions->row->is_remit == 0) {
                    $actions->append(new CheckToRemit($actions->getKey()));
                }elseif($actions->row->is_confirm == 3 && $actions->row->is_send_back == 0){
                    $actions->append(new CheckToCancel($actions->getKey()));
                }
            });
        });

        $show->panel()->tools(function ($tools) {
            $tools->disableDelete();
            $tools->disableEdit();
        });

        return $show;
    }

    //完成确认
    public function achieve(Request $request)
    {
        $id = $request->post('id');

        $update['is_achieve_confirmed'] = '1';

        $res = Order::where('id', $id)->update($update);

        if(!$res) {
            return ['code' => 'false', 'msg' => '修改失败'];
        }

        return ['code' => 'true', 'msg' => '修改成功'];
    }


    //打款/取消
    public function RemitCancel(Request $request)
    {
        $id = $request->post('id');
        $status = $request->post('status');

        $order = Order::where('id', $id)->with('phone')->first();
        foreach ($order->phone as $k => $v) {
            if($v->is_confirm == 2) {
                if($v->is_remit == 0) {
                    return ['code' => 'false', 'msg' => '存在未结清子订单，请先结清子订单'];
                }
            }elseif($v->is_confirm == 3){
                if($v->is_send_back == 0) {
                    return ['code' => 'false', 'msg' => '存在未结清子订单，请先结清子订单'];
                }
            }
        }

        if($status == '1') {
            $update['is_remit'] = '1';
        }else{
            $update['is_cancel'] = '1';
        }


        $res = Order::where('id', $id)->update($update);

        if(!$res) {
            return ['code' => 'false', 'msg' => '修改失败'];
        }

        return ['code' => 'true', 'msg' => '修改成功'];
    }

    //打款/取消
    public function ToRemit(Request $request)
    {
        $id = $request->post('id');

        $update['is_remit'] = '1';


        $res = OrderPhone::where('id', $id)->update($update);

        if(!$res) {
            return ['code' => 'false', 'msg' => '修改失败'];
        }

        return ['code' => 'true', 'msg' => '修改成功'];
    }

    //打款/取消
    public function ToCancel(Request $request)
    {
        $id = $request->post('id');

        $info = OrderPhone::where('id', $id)->first();
        if(empty($info['logistics_company']) || empty($info['tracking_number'])) {
            return ['code' => 'false', 'msg' => '请先填写寄回物流信息'];
        }

        $update['is_send_back'] = '1';

        $res = OrderPhone::where('id', $id)->update($update);

        if(!$res) {
            return ['code' => 'false', 'msg' => '修改失败'];
        }

        return ['code' => 'true', 'msg' => '修改成功'];
    }
}
